package com.ssm.demo.util.common;


import com.ssm.demo.util.global.GlobalExceptionEnum;
import lombok.Data;
import lombok.ToString;

import java.util.HashMap;


/**
 * 描述： 通用返回对象
 */

@Data
@ToString
//@ApiModel("ApiRestResponse-通用的响应结果")
public class ApiRestResponse<T> {

//    @ApiModelProperty("状态码")
    private Integer code;

//    @ApiModelProperty("描述信息")
    private String description;

//    @ApiModelProperty("简单的返回结果")
    private String result;
//    @ApiModelProperty("详细的返回结果")
    private T properties;

    private static final int OK_CODE = 1;

    private static final String OK_MSG = "SUCCESS";

    public ApiRestResponse(Integer code, String description, T properties) {
        this.code = code;
        this.description = description;
        this.properties = properties;
    }

    @SuppressWarnings("unchecked")
    public ApiRestResponse(Integer code, String description) {
        this.code = code;
        this.description = description;
        this.properties = (T) new HashMap<Object, Object>(0);
    }

    public ApiRestResponse() {
        this(OK_CODE, OK_MSG);
    }

    public static <T> ApiRestResponse<T> success() {
        return new ApiRestResponse<>();
    }

    public static <T> ApiRestResponse<T> success(T properties) {
        ApiRestResponse<T> response = new ApiRestResponse<>();
        response.setProperties(properties);
        return response;
    }

    public static <T> ApiRestResponse<T> error(Integer code, String msg) {
        return new ApiRestResponse<>(code, msg);
    }

    public static <T> ApiRestResponse<T> error(GlobalExceptionEnum ex) {
        return new ApiRestResponse<>(ex.getCode(), ex.getMsg());
    }
}
